// vario.h
//

#ifndef __VARIO_H__
#define __VARIO_H__

#include <math.h>


/***************************************************************************************/
/* sensitivity / data filtering / Kalman filter parameters                             */
/* The Kalman Filter is being used to remove noise from the MS5611 sensor data. It can */
/* be adjusted by changing the value Q (process Noise) and R (Sensor Noise).           */
/* Best pactise is to only change R, as both parametrers influence each other.         */
/* (less Q requires more R to compensate and vica versa)                               */
/***************************************************************************************/
#define KALMAN_Q 0.05 // 0.05 do not tamper with this value unless you are looking for adventure ;-)
#define KALMAN_R 300  // default:300   change this value if you want to adjust the default sensitivity!
// this will only be used if PIN_PPM is NOT defined
//  50 = fast but lot of errors (good for sensor testing...but quite a lot of noise)
// 300 = medium speed and less false indications (nice medium setting with
//       a silence window (indoor)of -0.2 to +0.2)
// 1000 = conservative setting ;-)
// 500 = still useable maybe for a slow flyer?
// 1000 and q=0.001 is it moving at all?? in stable airpressure you can measure
//      the height of your furniture with this setting. but do not use it for flying ;-)
// .. everything inbetween in relation to these values.

#define OFFSET_PRESSURE					(0)
#define OFFSET_TEMPERATURE				(0)  // (-80)

#define QUEUE_SIZE_PRESSURE				(6)
#define QUEUE_SIZE_CLIMBRATE			(5)

typedef struct tagVARIOSTATE
{
	// Pressure
	long			pressure_queue[QUEUE_SIZE_PRESSURE];
	char			pressure_index;
	long			pressure_current;
	
	// Climb Rate
	float			climbrate_queue[QUEUE_SIZE_CLIMBRATE];
	char			climbrate_index;
	float			climbrate_current;
	
	//
	unsigned long	timer_last;
	unsigned long	timer_climbrate;

	//
	float			altitute_current;
	float			altitute_last;

	//
	float			temperature_last;
	
	//	int				deviation;
	
	double			kalman_q;
	double			kalman_r;
	float			kalman_x;
	
} VarioState;


void vario_setup();
void vario_loop();

void saveClimbRate(float altitute);
float getClimbRate();
float getAltitude(long pressure, int temperature) ;

void savePressure(long pressure);
long getAveragePressure();
long getPressure();
float kalman_update_local(float measurement);


#endif // __VARIO_H__